﻿using System;
using System.Windows.Forms;
using Sponsorship.Domain;
using Sponsorship.Domain.Persons;
using Sponsorship.Infrastructure;

namespace Sponsorship.Ui.Modules.Child
{
    public partial class EnterSponsoredPersonForm : Form
    {
        private readonly IBus _bus;

        public EnterSponsoredPersonForm(IBus bus)
        {
            _bus = bus;
            InitializeComponent();
        }

        private void SaveButtonClick(object sender, EventArgs e)
        {
            var sponsoredPersonId = Guid.NewGuid();
            var command = new EnterPerson (
                    sponsoredPersonId, 
                    _firstNameTextBox.Text, 
                    _lastNameTextBox.Text, 
                    _dateOfBirthPicker.Checked ? (DateTime?) _dateOfBirthPicker.Value : null,
                    "",
                    SponsoringStatus.NotSponsorable,
                    "");
            _bus.Send(command);

            //Add an associated person to the child - no GUI for now - as a sample how to update an aggregate and the read side
            //var personId = Guid.NewGuid();
            //_bus.Send (new EnterPerson (personId, "Tommy", "Kakai", new DateTime (1980, 1, 1)));
            //_bus.Send(new AssociatePerson (sponsoredChildId, 1, personId, "son"));

            DialogResult = DialogResult.OK;
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void ControlKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                // Select next control (simulates Tab)
                SelectNextControl((Control)sender, true, true, true, true);
                // Prevent beep
                e.Handled = true;
            }
        }
    }
}
